import 'package:flutter_nb_net/flutter_net.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../../../common/log/x_logger.dart';
import '../../../provider/global_provider.dart';
import '../../home/home/model/home_device_info_resp.dart';

mixin LoginPageMixin<T extends ConsumerStatefulWidget> on ConsumerState<T> {
  Future refreshDeviceInfo( success()) async {
    XLogger.debug('刷新设备信息 --- ');
    final deviceInfo = ref.watch(deviceInfoProvider);
    int? oldDeviceMode = deviceInfo.deviceMode;

    var res = await get<HomeDeviceInfoResp, HomeDeviceInfoResp>(
        '/home/deviceInfo',
        queryParameters: {'imei': deviceInfo.imei ?? ""},
        decodeType: HomeDeviceInfoResp());
    res.when(
        success: (HomeDeviceInfoResp resp) async {
          XLogger.info(res.toString());
          if (resp.code == 200) {
             if (resp.data == null || resp.data!.deviceInfo == null) {
              ref.read(deviceInfoProvider.notifier).removeDeviceInfo();
               return;
            }

            var deviceInfoVO = resp.data!.deviceInfo!;
            deviceInfoVO.deviceMode = oldDeviceMode;
            ref.read(deviceInfoProvider.notifier).setDeviceInfo(deviceInfoVO);
           } else if (resp.code == 60001) {
            // 无设备信息，移除显示设备
            ref.read(deviceInfoProvider.notifier).removeDeviceInfo();
           } else {
            // setState(() {
            //   hasThing = false;
            // });
            XLogger.debug(resp.msg);
          }
          success();
        },
        failure: (String msg, int code) {
          success();
        });
  }
}
